package com.xiyuan.config;

import com.xiyuan.smartutils.Resources;
import com.xiyuan.smartutils.constants.SysConstants;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;

/**
 * 工程参数配置类的DTD本地验证，加载包内的文件(boot_config_1_0_0.dtd)，这样则不要到网站下载验证 <br>
 * 
 * @version v1.0.0 @author lgz 2017-8-27  新建与整理
 */
public class ConfigResolver implements EntityResolver, SysConstants
{
    private String path;
    
    public ConfigResolver(String path)
    {
        this.path = path;
    }
    
    /**
     * 实现验证流
     * 
     * @see EntityResolver#resolveEntity(String, String)
     */
    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
    {
        if (!CONFIG_DTD_PUBLIC_ID.equals(publicId))
        {//版本不支持
            throw new SAXException("配置文件["+path+"]格式不正确，["+VERSION+"]仅支持["+CONFIG_DTD_PUBLIC_ID+"]格式");
        }
        
        InputStream stream = Resources.getResourceStream(ConfigResolver.class, CONFIG_DTD_PATH);
        return new InputSource(stream);
    }
}
